

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas" width="500px" height="500px"></canvas>
    <script>
        //开启canvas代码提示
        /** @type {HTMLCanvasElement} */
        // 1.获取画布
        let canvas = document.getElementById('canvas')
        // 2.获取画布的上下文
        let ctx = canvas.getContext('2d')


        for (let i = 0; i < 20; i++) {
            drawLine(100 + 10 * i, 100, 105 + 10 * i, 100, 'red', 2)
        }

        for (let i = 0; i < 20; i++) {
            drawLine(100 + 10 * i, 100 + 10 * i, 105 + 10 * i, 100 + 10 * i, 'green', 2)
        }



        /**
         * @description: canvas画直线
         * @param {*} x1
         * @param {*} y1
         * @param {*} x2
         * @param {*} y2
         * @param {*} color
         * @param {*} width
         */
        function drawLine(x1, y1, x2, y2, color, width) {
            ctx.beginPath();
            ctx.moveTo(x1, y1)
            ctx.lineTo(x2, y2)
            ctx.strokeStyle = color
            ctx.lineWidth = width
            ctx.stroke()
            ctx.closePath()
        }
    </script>
</body>

</html>